home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSFILE2.C < prev    next >
C/C++ Source or Header  |  1995-08-04  |  5KB  |  172 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsfile2.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to read text files. It roughly approximates
  25. //    fgets() and the other streaming functions. However, this functions use
  26. //    the base library file function so that critical error and stuff are
  27. //    handled correctly.
  28. //
  29. //    This class is a prime candidate for C++ however it is needed on the 
  30. //    SCO UNIX platform to read configuration files.
  31. //
  32. //    The code in this module should be written entirely in C. 
  33. //    Do not use any C++ constructs.
  34. //
  35. //
  36. //    The code in this module should be written entirely in C. 
  37. //    Do not use any C++ constructs.
  38. //
  39. //    This module is portable to:
  40. //        DOS 3.X+
  41. //        MS Windows 3.X+
  42. //        MS Windows NT
  43. //        OS/2 2.X+
  44. //        OS/2 2.0 PM
  45. //        SCO UNIX.
  46. //
  47. //    The following compilers are supported:
  48. //        MSC 6.0A
  49. //        MSC/C++ 7.0
  50. //        Borland C++ 3.1 for DOS
  51. //        Borland C++ 1.0 for OS/2 2.X
  52. //        SCO UNIX cc
  53. //
  54. //----------------------------------------------------------------------------
  55. #include <bs.h>
  56.  
  57.  
  58. //----------------------------------------------------------------------------
  59. //   Description:    Get file name.
  60. //    Parameters:    hf            File handle.
  61. //       Returns:    Pointer to file name
  62. //----------------------------------------------------------------------------
  63. ULONG FN_E FileLine(HF hf)
  64. {
  65.     PBS_HF pbs_hf = FileHandle(hf);
  66.     return pbs_hf == NULL ? 0L: pbs_hf->ulLine;
  67. }
  68.  
  69.  
  70. //----------------------------------------------------------------------------
  71. //   Description:    Get file name.
  72. //    Parameters:    hf            File handle.
  73. //       Returns:    Pointer to file name
  74. //----------------------------------------------------------------------------
  75. PCSZ FN_E FileName(HF hf)
  76. {
  77.     PBS_HF pbs_hf = FileHandle(hf);
  78.     return pbs_hf == NULL || !pbs_hf->pcszName[0] ? "unknown": pbs_hf->pcszName;
  79. }
  80.  
  81.  
  82. //----------------------------------------------------------------------------
  83. //   Description:    Read a line of text from an input file.
  84. //    Parameters:    hf        File handle
  85. //                        pch    Buffer to receive text
  86. //                        cch    Size of buffer. 
  87. //       Returns:    TRUE if successful and a line of text was found.
  88. //----------------------------------------------------------------------------
  89. BOOL FN_E FileReadStr(HF hf, PCHAR pch, SIZET cch)
  90. {
  91.     PBS_HF pbs_hf = FileHandle(hf);
  92.  
  93.     Assert(pch && cch);
  94.     if (pbs_hf == NULL
  95.     || pbs_hf->fError
  96.     || ((pbs_hf->fsize - pbs_hf->fpos) <= 0
  97.     && (!pbs_hf->cDat || pbs_hf->cOff >= pbs_hf->cDat)))
  98.         return FALSE;
  99.  
  100.     if (!BTEST(pbs_hf->fl, FL_BUFFERED))
  101.         return FileError(hf, "File is not buffered.");
  102.  
  103.     pbs_hf->ulLine++;
  104.  
  105.     while (cch > 1)
  106.         {                                            // Nothing left?
  107.         FPOS ffile = pbs_hf->fsize - pbs_hf->fpos;
  108.  
  109.         if (ffile <= 0
  110.         && (!pbs_hf->cDat || pbs_hf->cOff >= pbs_hf->cDat))
  111.             break;
  112.  
  113.         if (!pbs_hf->cDat                            // Buffer empty
  114.         || pbs_hf->cOff >= pbs_hf->cDat)        // Or buffer used up
  115.             {
  116.             SIZET cRead;                        // Read another buffer full
  117.  
  118.             cRead = (SIZET)MIN((FPOS)pbs_hf->cBuf, (FPOS)ffile);
  119.             if (!FileRead(hf, (PSZ)pbs_hf->pcszBuf, cRead, (FPOS)-1L))
  120.                 {
  121.                 pbs_hf->fError = TRUE;
  122.                 return FALSE;
  123.                 }
  124.             pbs_hf->cDat = cRead;                // Adjust globals
  125.             pbs_hf->cOff = 0;
  126.             }
  127.         for (; pbs_hf->cOff < pbs_hf->cDat && cch > 1; ++pbs_hf->cOff)
  128.             if (pbs_hf->pcszBuf[pbs_hf->cOff] == '\n')
  129.                 {
  130.                 pbs_hf->cOff++;
  131.                 goto EXIT_HERE;
  132.                 }
  133.             else if (pbs_hf->pcszBuf[pbs_hf->cOff] != '\r')
  134.                 {
  135.                 *pch++ = pbs_hf->pcszBuf[pbs_hf->cOff];
  136.                 cch--;
  137.                 }
  138.         }
  139. EXIT_HERE:
  140.     pch[0] = '\0';
  141.     return TRUE;
  142. }
  143.  
  144.  
  145. //----------------------------------------------------------------------------
  146. //   Description:    Run standard test suite
  147. //    Parameters:    sTest        Test to run.
  148. //                                        0        Run all default tests (except).
  149. //       Returns:    TRUE if successful.
  150. //----------------------------------------------------------------------------
  151. #if COMPILE_TEST
  152. BOOL FN TextInTest(SHORT sTest)
  153. {
  154.     HF hf;
  155.     CHAR szBuffer[256];
  156.  
  157.     NOTUSED(sTest);
  158.     Output("Read config.sys as text file:\n");
  159.     if (!FileOpen(&hf, "c:\\config.sys", FL_O_RT, NULL))
  160.         return FALSE;
  161.  
  162.     while (FileReadStr(hf, szBuffer, sizeof(szBuffer)))
  163.         Output(" %05ld: [%s]\n", FileLine(hf), szBuffer);
  164.  
  165.     FileClose(hf);
  166.     return TRUE;
  167. }
  168. #endif
  169. //----------------------------------------------------------------------------
  170. //------------------------------- End of File --------------------------------
  171. //----------------------------------------------------------------------------
  172.